﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;

using ConfigDashboard.Models;

namespace ConfigDashboard.Controllers
{
    public class SettingsController : Controller
    {
        private ConfigSettingsEntities db = new ConfigSettingsEntities();

        //
        // GET: /Settings/

        public ActionResult Index()
        {
            return View(db.ConfigSettingNames.ToList());
        }

        //
        // GET: /Settings/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Settings/Create

        [HttpPost]
        public ActionResult Create(ConfigSettingName configsettingname)
        {
            if (ModelState.IsValid)
            {
                db.ConfigSettingNames.AddObject(configsettingname);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(configsettingname);
        }

        //
        // GET: /Settings/Edit/5

        public ActionResult Edit(int id = 0)
        {
            ConfigSettingName configsettingname = db.ConfigSettingNames.Single(c => c.SettingID == id);
            if (configsettingname == null)
            {
                return HttpNotFound();
            }
            return View(configsettingname);
        }

        //
        // POST: /Settings/Edit/5

        [HttpPost]
        public ActionResult Edit(ConfigSettingName configsettingname)
        {
            if (ModelState.IsValid)
            {
                db.ConfigSettingNames.Attach(configsettingname);
                db.ObjectStateManager.ChangeObjectState(configsettingname, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(configsettingname);
        }

        //
        // GET: /Settings/Delete/5

        public ActionResult Delete(int id = 0)
        {
            ConfigSettingName configsettingname = db.ConfigSettingNames.Single(c => c.SettingID == id);
            if (configsettingname == null)
            {
                return HttpNotFound();
            }
            return View(configsettingname);
        }

        //
        // POST: /Settings/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            ConfigSettingName configsettingname = db.ConfigSettingNames.Single(c => c.SettingID == id);
            db.ConfigSettingNames.DeleteObject(configsettingname);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        /// <summary>
        /// Releases unmanaged resources and optionally releases managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}